<!--
    group: 发票管理
    name: 申请开票/开票列表
    item: 发票领用公共组件
-->
<template>
  <div>
    <a-modal
      :width="800"
      :footer="null"
      :maskClosable="false"
      @cancel="cancle"
      v-model="isUseShow"
    >
      <div class="invoiceReceive" >
        <h4>发票余量不足，请选择发票领用数量！</h4>

        <div v-for="item in invoiceNameList" v-if="item.surplusList.length > 0" class="invoiceItemBox">
          <div :class="[item.taxType == taxType && item.taxMedium == taxMedium ? 'red' : '']" class="title">
            增值税{{ item.taxType == 1 ? '专用' : '普通' }}发票-{{ item.taxMedium == 1 ? '纸质' : '电子' }}票
          </div>
          申请领用张数：
          <a-select placeholder="请选择" style="width: 100px" v-model="billSegment[item.name]">
            <a-select-option
              v-for="i in item.surplusList"
              :value="i"
            >
              {{ i }}
            </a-select-option>
          </a-select>
        </div>
      </div>
      <div class="desc_btn">
        <a-button type="primary" class="btnsLarge" @click="receiveNext">下一步</a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
export default {
  name: "usageInsufficient",
  props: ['isUseShow','taxType','taxMedium','ticketMargin','billUseNum'],
  data(){
    return{
      show: false,
      //发票领用数据
      billSegment:{
        zd: null,
        zz: null,
        pd: null,
        pz: null,
      },
      invoiceNameList: [
        { name:'zd',taxType:'1',taxMedium:'2',surplusList: [] },  //专用发票-电子
        { name:'zz',taxType:'1',taxMedium:'1',surplusList: [] },  //专用发票-纸质
        { name:'pd',taxType:'2',taxMedium:'2',surplusList: [] },  //普通发票-电子
        { name:'pz',taxType:'2',taxMedium:'1',surplusList: [] },  //普通发票-纸质
      ],
    }
  },
  watch:{
    'ticketMargin':{
      immediate: true,
      deep: false,  //深度监听
      handler(val){
        let a,b,c,d;
        //领用数弹层 ==>  本次开票数-本次剩余
        //除本次票种：本次开票数-本次剩余    其他非本次开的票种：0-本次剩余
        //专用发票-电子
        if (this.taxType == 1 && this.taxMedium == 2){
          a = this.billUseNum - this.ticketMargin.zd;
          this.billSegment.zd = a;
          0 - this.ticketMargin.zz > 0 ? b = 0 - this.ticketMargin.zz : b = 0;
          0 - this.ticketMargin.pd > 0 ? c = 0 - this.ticketMargin.pd : c = 0;
          0 - this.ticketMargin.pz > 0 ? d = 0 - this.ticketMargin.pz : d = 0;
        }
        //专用发票-纸质
        else if (this.taxType == 1 && this.taxMedium == 1){
          b = this.billUseNum - this.ticketMargin.zz;
          this.billSegment.zz = b;
          0 - this.ticketMargin.zd > 0 ? a = 0 - this.ticketMargin.zd : a = 0;
          0 - this.ticketMargin.pd > 0 ? c = 0 - this.ticketMargin.pd : c = 0;
          0 - this.ticketMargin.pz > 0 ? d = 0 - this.ticketMargin.pz : d = 0;
        }
        //普通发票-电子
        else if (this.taxType == 2 && this.taxMedium == 2){
          c = this.billUseNum - this.ticketMargin.pd;
          this.billSegment.pd = c;
          0 - this.ticketMargin.zd > 0 ? a = 0 - this.ticketMargin.zd : a = 0;
          0 - this.ticketMargin.zz > 0 ? b = 0 - this.ticketMargin.zz : b = 0;
          0 - this.ticketMargin.pz > 0 ? d = 0 - this.ticketMargin.pz : d = 0;
        }
        //普通发票-纸质
        else if (this.taxType == 2 && this.taxMedium == 1){
          d = this.billUseNum - this.ticketMargin.pz;
          this.billSegment.pz = d;
          0 - this.ticketMargin.zd > 0 ? a = 0 - this.ticketMargin.zd : a = 0;
          0 - this.ticketMargin.zz > 0 ? b = 0 - this.ticketMargin.zz : b = 0;
          0 - this.ticketMargin.pd > 0 ? c = 0 - this.ticketMargin.pd : c = 0;
        }else{
          //0 - a > 0 ? val = 0 - a : val = 0
        }

        //处理组成数组
        this.invoiceNameList[0].surplusList = this.funSumList(a,this.ticketMargin.zdSurplusGet);
        this.invoiceNameList[1].surplusList = this.funSumList(b,this.ticketMargin.zzSurplusGet);
        this.invoiceNameList[2].surplusList = this.funSumList(c,this.ticketMargin.pdSurplusGet);
        this.invoiceNameList[3].surplusList = this.funSumList(d,this.ticketMargin.pzSurplusGet);
      }
    }
  },
  methods:{
    //验证选择领取张数是否超限制
    verifyUseNumber(){
      //本次使用张数+5≤申请领用张数≤本月可用余量
      //先区分本次开票票种
      if (this.taxType == 1 && this.taxMedium == 2){
        //专票电子
        if (this.billSegment.zd){
          // console.log('申请张数==> ' + this.billSegment.zd)
          // console.log('已领张数==> ' + this.ticketMargin.zd)
          // console.log('最大剩余==> ' + this.ticketMargin.zdSurplusGet)
          // console.log('用户开票数==> ' + this.billUseNum)
          // 申请领用张数 <= 本月可用余量
          if (this.billSegment.zd <= this.ticketMargin.zdSurplusGet){
            return true;
          }else{
            // 专用发票-电子票
            this.$message.warning('申请领用张数应小于本月最大可用余量');
            return false;
          }
        }else{
          this.ticketVerificationRules();
        }
      }else if (this.taxType == 1 && this.taxMedium == 1){
        //专票纸质
        if (this.billSegment.zz){
          if (this.billSegment.zz <= this.ticketMargin.zzSurplusGet){
            return true;
          }else{
            // 专用发票-纸质票
            this.$message.warning('申请领用张数应小于本月最大可用余量');
            return false;
          }
        }else{
          this.ticketVerificationRules();
        }
      }else if (this.taxType == 2 && this.taxMedium == 2){
        //普票电子
        if (this.billSegment.pd){
          if (this.billSegment.pd <= this.ticketMargin.pdSurplusGet){
            return true;
          }else{
            // 普通发票-电子票
            this.$message.warning('申请领用张数应小于本月最大可用余量');
            return false;
          }
        }else{
          this.ticketVerificationRules();
        }
      }else if (this.taxType == 2 && this.taxMedium == 1){
        //普票纸质
        if (this.billSegment.pz){
          if (this.billSegment.pz <= this.ticketMargin.pzSurplusGet){
            return true;
          }else{
            // 普通发票-纸质票
            this.$message.warning('申请领用张数应小于本月最大可用余量');
            return false;
          }
        }else{
          this.ticketVerificationRules();
        }
      }
    },
    //票校验规则
    ticketVerificationRules(){
      if (this.billSegment.zd){
        //专票电子
        if (this.billSegment.zd <= this.ticketMargin.zdSurplusGet){
          return true;
        }else{
          this.$message.warning('专用发票-电子票申请领用张数应小于本月可用余量');
          return false;
        }
      }
      if (this.billSegment.zz){
        //专票纸质
        if (this.billSegment.zz <= this.ticketMargin.zzSurplusGet){
          return true;
        }else{
          this.$message.warning('专用发票-纸质票申请领用张数应小于本月可用余量');
          return false;
        }
      }
      if (this.billSegment.pd){
        //普票电子
        if (this.billSegment.pd <= this.ticketMargin.pdSurplusGet){
          return true;
        }else{
          this.$message.warning('普通发票-电子票申请领用张数应小于本月可用余量');
          return false;
        }
      }
      if (this.billSegment.pz){
        //普票纸质
        if (this.billSegment.pz <= this.ticketMargin.pzSurplusGet){
          return true;
        }else{
          this.$message.warning('普通发票-纸质票申请领用张数应小于本月可用余量');
          return false;
        }
      }
    },
    //下一步
    receiveNext(){
      let numStatus = this.verifyUseNumber();
      if (numStatus){
        this.$emit('handleUseFather',this.billSegment);
      }
    },
    //领用弹层取消
    cancle(){
      this.$parent.collecteCancle();
    },
    //计算数字分成数组
    funSumList(newIn,num){
      let arr = [];
      for (let i = newIn; i <= num; i++) {
        arr.push(i)
      }
      return arr.length > 1 ? arr : [];
    }
  }
}
</script>

<style scoped lang="less">
.invoiceReceive{
  width: 100%;
  padding-left: 30px;
  h4{
    font-weight: bolder;
    font-size: 20px;
    margin-bottom: 20px;
  }
  .invoiceItemBox{
    display: flex;
    align-items: center;
    color: #333333;
    font-size: 16px;
    margin-top: 15px;
    .title{
      font-weight: bolder;
    }
  }

  p {
    color: @color-smudge;
    font-size: 16px;

    span {
      font-weight: bolder;
    }
  }
}

.desc_btn{
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  margin: 40px 0 20px 0;
  text-align: center;
}
.desc_btn a{
  margin-left: 10px;
  //text-decoration: underline;
}
</style>
